package com.ruoyi.erp.service.impl;

import java.math.BigDecimal;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.erp.domain.*;
import com.ruoyi.erp.service.IBillSettleService;
import com.ruoyi.erp.service.IStaffingMonthBillService;
import com.ruoyi.erp.service.IStaffingPoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.BillSettleDetailMapper;
import com.ruoyi.erp.service.IBillSettleDetailService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 结算明细Service业务层处理
 * 
 * @author sauger
 * @date 2022-03-25
 */
@Service
public class BillSettleDetailServiceImpl implements IBillSettleDetailService 
{
    @Autowired
    private BillSettleDetailMapper billSettleDetailMapper;
    @Autowired
    private IStaffingMonthBillService staffingMonthBillService;
    @Autowired IStaffingPoService staffingPoService;

    /**
     * 查询结算明细
     * 
     * @param settleId 结算明细主键
     * @return 结算明细
     */
    @Override
    public BillSettleDetail selectBillSettleDetailBySettleId(Integer settleId)
    {
        return billSettleDetailMapper.selectBillSettleDetailById(settleId);
    }

    /**
     * 查询结算明细列表
     * 
     * @param billSettleDetail 结算明细
     * @return 结算明细
     */
    @Override
    public List<BillSettleDetail> selectBillSettleDetailList(BillSettleDetail billSettleDetail)
    {
        return billSettleDetailMapper.selectBillSettleDetailList(billSettleDetail);
    }

    /**
     * 新增结算明细
     * 
     * @param billSettleDetail 结算明细
     * @return 结果
     */
    @Override
    public int insertBillSettleDetail(BillSettleDetail billSettleDetail)
    {
        billSettleDetail.setCreateTime(DateUtils.getNowDate());
        return billSettleDetailMapper.insertBillSettleDetail(billSettleDetail);
    }

    /**
     * 修改结算明细
     * 
     * @param billSettleDetail 结算明细
     * @return 结果
     */
    @Override
    public int updateBillSettleDetail(BillSettleDetail billSettleDetail)
    {
        return billSettleDetailMapper.updateBillSettleDetail(billSettleDetail);
    }

    /**
     * 批量删除结算明细
     * 
     * @param settleIds 需要删除的结算明细主键
     * @return 结果
     */
    @Override
    public int deleteBillSettleDetailByIds(String ids)
    {
        return billSettleDetailMapper.deleteBillSettleDetailBySettleIds(Convert.toStrArray(ids));
    }

    /**
     * 删除结算明细信息
     * 
     * @param settleId 结算明细主键
     * @return 结果
     */
    @Override
    public int deleteBillSettleDetailById(Integer settleId)
    {
        return billSettleDetailMapper.deleteBillSettleDetailById(settleId);
    }

    /**
     * 根据结算id 获取结算账单明细
     *
     * @param settleId
     * @return
     */
    @Override
    public List<SettleMonthBill> selectSettleMonthBillBySettleId(Integer settleId) {
        return billSettleDetailMapper.selectSettleMonthBillBySettleId(settleId);
    }

    /**
     * 根据结算id，获取结算po明细
     *
     * @param settleId
     * @return
     */
    @Override
    public List<SettlePo> selectSettlePoBySettleId(Integer settleId) {

        return billSettleDetailMapper.selectSettlePoBySettleId(settleId);
    }

    /**
     * @param bill
     * @return
     */
    @Override
    @Transactional
    public int insertSettleBillMonthBill(Integer settleId,SettleMonthBill bill) {
        BillSettleDetail detail = new BillSettleDetail();
        detail.setSettleId(settleId);
        detail.setResourceId(bill.getResourceId());
        detail.setResourceType("bill");
        detail.setAmount(bill.getAmount());
        return insertBillSettleDetail(detail);
    }

    @Override
    public int insertSettleBillPo(Integer settleId,SettlePo po) {
        BillSettleDetail detail = new BillSettleDetail();
        detail.setSettleId(settleId);
        detail.setResourceId(po.getResourceId());
        detail.setResourceType("po");
        detail.setAmount(po.getAmount());
        detail.setPayedAmount(BigDecimal.ZERO);
        StaffingPo monthPo = staffingPoService.selectStaffingPoById(po.getResourceId());
        monthPo.setBilledAmount(monthPo.getBilledAmount().add(po.getAmount()));
        staffingPoService.updateStaffingPo(monthPo);
        return insertBillSettleDetail(detail);
    }

    @Override
    public int deleteSettleBillMonthBill(SettleMonthBill bill) {
        return deleteBillSettleDetailById(bill.getSettleDetailId());
    }

    @Override
    public int updateSetlleBillMonthBill(SettleMonthBill settleMonthBill) {
        BillSettleDetail detail = new BillSettleDetail();
        detail.setSettleDetailId(settleMonthBill.getSettleDetailId());
        detail.setAmount(settleMonthBill.getAmount());
        detail.setUpdateTime(DateUtils.getNowDate());
        return this.updateBillSettleDetail(detail);
    }

    @Override
    public int deleteSettleBillPo(SettlePo po) {
        po.setBilledAmount(po.getBilledAmount().subtract(po.getAmount()));
        staffingPoService.updateStaffingPo(po);
        return deleteBillSettleDetailById(po.getSettleDetailId());
    }

    @Override
    public int updateSettleBillPo(SettlePo existPo, SettlePo newPo) {
        BigDecimal deltaAmount = newPo.getAmount().subtract(existPo.getAmount());
        if (deltaAmount.equals(BigDecimal.ZERO)){
            //金额没变，不保存到数据库
            return existPo.getId();
        }else
        {
            existPo.setBilledAmount(existPo.getBilledAmount().add(deltaAmount));
            staffingPoService.updateStaffingPo(existPo);
            BillSettleDetail detail = new BillSettleDetail();
            detail.setSettleDetailId(existPo.getSettleDetailId());
            detail.setAmount(newPo.getAmount());
            detail.setUpdateTime(DateUtils.getNowDate());
            return this.updateBillSettleDetail(detail);
        }
    }
}
